package com.example.finalhomework.thirdhomework

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.finalhomework.R

@Composable
fun ThirdScreen_3(navController: NavHostController, onConfirmClick: (String) -> Unit) {
    // 使用 remember 保存输入的消息状态
    var message by remember { mutableStateOf("") }

    Column(
        modifier = Modifier
            .fillMaxSize(), // 填充整个屏幕
//            .padding(16.dp), // 设置内边距为16dp
        verticalArrangement = Arrangement.Center, // 垂直居中排列
        horizontalAlignment = Alignment.CenterHorizontally // 水平居中排列
    ) {

        Image(
            painter = painterResource(id = R.drawable.background5),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .height(300.dp)
        )
        // 输入框，用于输入消息
        OutlinedTextField(
            value = message, // 输入框的当前值
            onValueChange = { message = it }, // 更新输入框的值
            label = { Text("Please input something") } // 输入框的标签
        )
        Spacer(modifier = Modifier.height(16.dp)) // 间隔16dp的空白
        // 按钮，点击后调用 onConfirmClick 并跳转回到 MainScreen
        Button(onClick = {
            onConfirmClick(message) // 调用回调函数，传递输入的消息
            navController.popBackStack("thirdhomework", false) // 返回到 thirdhomework，不销毁当前目的地
            Log.d("ThirdScreen_3", "Confirm button clicked with message: $message")
        }) {
            Text("Confirm") // 按钮上的文本
        }

        Image(
            painter = painterResource(id = R.drawable.background4),
            contentDescription = null,
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
        )
    }
    Log.d("ThirdScreen_3", "Composable function executed")
}

@Preview(showBackground = true)
@Composable
fun ThirdScreenPreview() {
    val navController = rememberNavController()
    ThirdScreen_3(navController = navController, onConfirmClick = {})
}